Unit myWinCELib;{$mode objfpc}{$H+}

Interface

Uses
  Classes, Forms, SysUtils, Windows,
  myFileUtils;

Const
    TH32CS_SNAPHEAPLIST     = $1;
    TH32CS_SNAPPROCESS      = $2;
    TH32CS_SNAPTHREAD       = $4;
    TH32CS_SNAPMODULE       = $8;
    TH32CS_SNAPALL          = (TH32CS_SNAPHEAPLIST or TH32CS_SNAPPROCESS or
        TH32CS_SNAPTHREAD or TH32CS_SNAPMODULE);
    TH32CS_GETALLMODS       = $8000000;
    AC_LINE_OFFLINE         = $00;
    AC_LINE_ONLINE          = $01;
    AC_LINE_BACKUP_POWER    = $02;
    AC_LINE_UNKNOWN         = $FF;
    BATTERY_FLAG_HIGH       = $01;
    BATTERY_FLAG_LOW        = $02;
    BATTERY_FLAG_CRITICAL   = $04;
    BATTERY_FLAG_CHARGING   = $08;
    BATTERY_FLAG_NO_BATTERY = $80;
    BATTERY_FLAG_UNKNOWN    = $FF;
    BATTERY_PERCENTAGE_UNKNOWN = $FF;
    BATTERY_LIFE_UNKNOWN    = $FFFFFFFF;

Type
    POWER_INFO = Record
        ACLineStatus: Byte;
        BatteryFlag: Byte;
        BatteryLifePercent: Byte;
        Reserved1: Byte;
        BatteryLifeTime: DWORD;
        BatteryFullLifeTime: DWORD;
        Reserved2: Byte;
        BackupBatteryFlag: Byte;
        BackupBatteryLifePercent: Byte;
        Reserved3: Byte;
        BackupBatteryLifeTime: DWORD;
        BackupBatteryFullLifeTime: DWORD;
        BatteryVoltage: DWORD;
        BatteryCurrent: DWORD;
        BatteryAverageCurrent: DWORD;
        BatteryAverageInterval: DWORD;
        BatterymAHourConsumed: DWORD;
        BatteryTemperature: DWORD;
        BackupBatteryVoltage: DWORD;
        BatteryChemistry: Byte;
    End;
    PPOWER_INFO = ^POWER_INFO;

    TagPROCESSENTRY32 = Record
        DwSize: DWORD;
        CntUsage: DWORD;
        Th32ProcessID: DWORD;
        Th32DefaultHeapID: DWORD;
        Th32ModuleID: DWORD;
        CntThreads: DWORD;
        Th32ParentProcessID: DWORD;
        PcPriClassBase: LONG;
        DwFlags: DWORD;
        SzExeFile: Array[0..MAX_PATH - 1] Of Wchar;
        Th32MemoryBase: DWORD;
        Th32AccessKey: DWORD;
    End;
    PROCESSENTRY32 = ^TagPROCESSENTRY32;

Function WaitForSingleObjectEx(HHandle: Handle; DwMilliseconds: DWORD): DWORD;
    External 'coredll.dll' Name 'WaitForSingleObject';

Function CreateToolhelp32Snapshot(Flags: Dword; IPID: Dword): Cardinal;
    Stdcall; External 'toolhelp.dll';

Function CloseToolhelp32Snapshot(Hsnap: Cardinal): Bool; Stdcall;
    External 'toolhelp.dll';

Function Process32First(Hsnap: Cardinal; Pe: PROCESSENTRY32): Bool;
    Stdcall; External 'toolhelp.dll';

Function Process32Next(Hsnap: Cardinal; Pe: PROCESSENTRY32): Bool;
    Stdcall; External 'toolhelp.dll';

Function GetSystemPowerStatusEx2(Var POutput: POWER_INFO;
    DwLen: DWORD; FUpdate: Boolean): BOOL;
    Stdcall; External 'coredll.dll' Name 'GetSystemPowerStatusEx2';

Function WCE_PlaySound(X1: PWideChar; X2: HMODULE; X3: DWORD): Longbool;
    Stdcall; External 'coredll.dll' Name 'PlaySoundW';

Function IsProcessExist(HProcess: DWORD): Boolean;
Procedure EnumerateProcesses(Var Processes: TStringList);
Procedure PlaySound(FileName: String);
Function OSExecuteWait(FileName, Parameters: WideString): Boolean;
Function OSExecute(FileName, Parameters: WideString): Boolean;
Procedure MyShowWindow(WindowTitle: String);
Procedure ShowTaskBar;
Procedure MyHideWindow(WindowTitle: String);
Procedure HideTaskBar;
Function BatteryPercent: Integer;
Function BatteryACPluged: Boolean;
Function AppLoaded:Boolean;

Implementation

Function BatteryACPluged: Boolean;
Var
    Temp: POWER_INFO;
Begin
    GetSystemPowerStatusEx2(Temp, SizeOf(POWER_INFO), True);
    Exit(Temp.ACLineStatus = 1);
End;

Function BatteryPercent: Integer;
Var
    Temp: POWER_INFO;
Begin
    GetSystemPowerStatusEx2(Temp, SizeOf(POWER_INFO), True);
    If Temp.BatteryFlag < 4 Then Exit(Temp.BatteryLifePercent);
    If Temp.BackupBatteryFlag < 4 Then Exit(Temp.BackupBatteryLifePercent);
    Exit(0);
End;

Procedure PlaySound(FileName: String);
Begin
    WCE_PlaySound(PWideChar(UTF8Decode(FileName)), 0, $20001);
End;

Function OSExecuteWait(FileName, Parameters: WideString): Boolean;
Var
    Info: SHELLEXECUTEINFO;
    Ret: DWORD;
Begin
    FillChar(Info, SizeOf(Info), 0);
    Info.CbSize := SizeOf(Info);
    Info.LpFile := PWideChar(UTF8Decode(FileName));
    Info.LpVerb := Nil;
    Info.FMask := $440;
    Info.DwHotKey := 0;
    Info.HIcon := 0;
    Info.HInstApp := 0;
    Info.HkeyClass := 0;
    Info.HProcess := 0;
    Info.Hwnd := 0;
    Info.LpClass := Nil;
    Info.LpDirectory := Nil;
    Info.LpIDList := Nil;
    Info.LpParameters := PWideChar(UTF8Decode(Parameters));
    Info.NShow := SW_SHOW;
    ShellExecuteEx(@Info);
    If Info.HProcess < 32 Then Exit(False);
    Repeat
        Ret := WaitForSingleObject(Info.HProcess, 100);
        Application.ProcessMessages;
    Until
        Ret = WAIT_OBJECT_0;
    Exit(True);
End;

Function OSExecute(FileName, Parameters: WideString): Boolean;
Var
    Info: SHELLEXECUTEINFO;
Begin
    FillChar(Info, SizeOf(Info), 0);
    Info.CbSize := SizeOf(Info);
    Info.LpFile := PWideChar(UTF8Decode(FileName));
    Info.LpVerb := Nil;
    Info.FMask := $440;
    Info.DwHotKey := 0;
    Info.HIcon := 0;
    Info.HInstApp := 0;
    Info.HkeyClass := 0;
    Info.HProcess := 0;
    Info.Hwnd := 0;
    Info.LpClass := Nil;
    Info.LpDirectory := Nil;
    Info.LpIDList := Nil;
    Info.LpParameters := PWideChar(UTF8Decode(Parameters));
    Info.NShow := SW_SHOW;
    ShellExecuteEx(@Info);
    If Info.HProcess < 32 Then Exit(False);
    Exit(True);
End;

Function IsProcessExist(HProcess: DWORD): Boolean;
Var
    Snap: Cardinal;
    Pe: TagPROCESSENTRY32;
Begin
    Pe.DwSize :=
        SizeOf(Pe);
    Snap := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    If Snap <> Invalid_handle_value Then
    Begin
        If Process32first(Snap, @Pe) = True Then
        While (Pe.Th32ParentProcessID <> HProcess) and (HProcess <> Pe.Th32ProcessID) Do
        Begin
            If Process32next(Snap, @Pe) <> True Then
            Begin
                CloseToolhelp32Snapshot(Snap);
                Exit(False);
            End;
        End;
        CloseToolhelp32Snapshot(Snap);
        Exit(True);
    End
    Else Exit(False);
End;

Procedure EnumerateProcesses(Var Processes: TStringList);
Var
    Snap: Cardinal;
    Pe: TagPROCESSENTRY32;
Begin
    If Processes = Nil Then Processes := TStringList.Create;
    Processes.Clear;
    Pe.DwSize := SizeOf(Pe);
    Snap := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    If Snap <> Invalid_handle_value Then
    Begin
        If Process32first(Snap, @Pe) = True Then 
        Repeat
            Processes.Add(IntToHex(Pe.Th32ParentProcessID, 8) + ',' + 
                          IntToHex(Pe.Th32ProcessID, 8) + ',' + 
                          WideCharToString(Pe.SzExeFile));
            
        Until Process32next(Snap, @Pe) <> True;
        CloseToolhelp32Snapshot(Snap);
    End;
End;


Function AppLoaded:Boolean;
Var
    WndID: HWND;
Begin
    WndID := FindWindow(PWideChar(UTF16Str('powerskin')), Nil);
    Exit(WndID <> 0);
End;

Procedure MyHideWindow(WindowTitle: String);
Var
    WndID: HWND;
Begin
    WndID := FindWindow(PWideChar(WindowTitle), Nil);
    If WndID <> 0 Then
    Begin
        ShowWindow(WndID, SW_HIDE);
        EnableWindow(WndID, False);
    End;
End;

Procedure HideTaskBar;
Var
    WndID: HWND;
Begin
    WndID := FindWindow('HHTaskBar', '');
    If WndID <> 0 Then
    Begin
        ShowWindow(WndID, 0);
        EnableWindow(WndID, False);
    End;
End;

Procedure MyShowWindow(WindowTitle: String);
Var
    WndID: HWND;
Begin
    WndID := FindWindow(PWideChar(WindowTitle), Nil);
    If WndID <> 0 Then
    Begin
        ShowWindow(WndID, SW_SHOWNORMAL);
        EnableWindow(WndID, True);
    End;
End;

Procedure ShowTaskBar;
Var
    WndID: HWND;
Begin
    WndID := FindWindow('HHTaskBar', '');
    If WndID <> 0 Then
    Begin
        ShowWindow(WndID, 1);
        EnableWindow(WndID, True);
    End;
End;

End.
